package JDBC;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class jdbc {
    public static void main(String[] args) throws Exception {
        /*
        使用JDBC连接数据库的基本过程
        1 加载驱动
        2 使用驱动管理器与数据库建立连接，并获取Connnection对象
        3 通过Connection创建执行语句对象Statement
        4 通过执行语句执行SQL语句
        5 如果执行的DQL语句则会得到查询结果集ResultSet
        6 遍历结果集获取查询数据
        7 关闭数据库链接
         */
        //1 加载驱动
        //不同数据库提供的驱动类名字不同
        Class.forName("com.mysql.cj.jdbc.Driver");
        //21
        Connection connection= DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/mydb1?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root","123456"

        );
        System.out.println("成功链接数据库");
        //3
         Statement statement = connection.createStatement();

        /*
        4
        CREATE TABLE userinfo(
        id INT PRTMARY KEY AUTO_INCREMENT,
        username VARCHAR(30),
        password VARCHAR(30),
        nickname VARCHAR(30),
        age INT(3)
        )
         */
        String sql=" CREATE TABLE userinfo(" +
                   "  id INT PRTMARY KEY AUTO_INCREMENT," +
                   "  username VARCHAR(30)," +
                   "  password VARCHAR(30)," +
                   "  nickname VARCHAR(30)," +
                   "  age INT(3)" +
                   ")";
        //5

        statement.
                execute(sql);
        System.out.println("成功创建表");
        System.out.println("成功创建表");
        System.out.println("成功创建表");
        //6
        connection.close();
    }
}
